#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef int ElementType;
typedef struct ListNode{
  ElementType Data;//数据域
  struct ListNode* next;//下一个节点的地址
}Node, *PNode;

PNode CreateList()
{
  PNode PHead,PTail;
  PHead = (PNode)malloc(sizeof(Node));
  if(PHead==NULL)
  {
    printf("头节点内存空间开辟失败\n");
    exit(-1);
  }

  PTail = PHead;
  PTail->next = NULL;
  int i=0;
  int len = 0;
  printf("请输入存放节点的个数:->\n");
  scanf("%d",&len);
  for(i=0;i<len;i++)
  {
    PNode PNew;
    PNew=(PNode)malloc(sizeof(Node));
    if(PNew==NULL)
    {
      printf("创建新节点失败\n");
      exit(-1);
    }
    printf("请输入第%d个节点数据:->",i+1);
    scanf("%d",&(PNew->Data));
    PNew->next = NULL;
    PTail->next = PNew;
    PTail = PNew;
  }
  printf("链表创建成功\n");
  return PHead->next;//返回第一个含有数据节点的位置
}

void TraverSList(PNode List)
{
  printf("链表的值为:->\n");
  if(List==NULL)
  {
    printf("链表为空\n");
  }
  while(List!=NULL)
  {
    printf("%d ",List->Data);
    List = List->next;
  }
  printf("\n");
}
//反转链表
PNode ReverseList(PNode List)
{
  if(List==NULL) 
  {
    printf("链表为空\n");
  }
  PNode old_head = List,new_head=NULL,temp;
  while(old_head!=NULL)
  {
    temp = old_head->next;
    old_head->next = new_head;
    new_head = old_head;
    old_head = temp;
  }
  return new_head;
}
int main()
{
  PNode List = CreateList();
  TraverSList(List);
  TraverSList(ReverseList(List));
  return 0;
}
